#include "mainmenu.h"
#include "ui_mainmenu.h"

MainMenu::MainMenu(QWidget *parent, QString newFilepath) :
    QWidget(parent),
    ui(new Ui::MainMenu)
{
    ui->setupUi(this);

    this->setGeometry(0, 0, 640, 640);
    this->filepath = newFilepath;

    //Create Background Image
    QString background = newFilepath + "Images/menubg.png";
    bg = QImage(background);

    //Load Font
    QFontDatabase::addApplicationFont(newFilepath + "Qubix.ttf");
    buttonFont = new QFont("Qubix", 28, -1, false);

    //Create Buttons
    newGame = new TextButton(this, "- New Game", "white", "#52E6FF");
    newGame->setFont(*buttonFont);
    newGame->setGeometry(348, 342, 227, 34);  //With font size of 28, width is ((letters + spaces/2) * 25 + 2)
    connect(newGame, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_newGame_clicked()));
    newGame->show();

    loadGame = new TextButton(this, "- Load Game", "white", "#3DE3FF");
    loadGame->setFont(*buttonFont);
    loadGame->setGeometry(348, 382, 252, 34);
    connect(loadGame, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_loadGame_clicked()));
    loadGame->show();

    settings = new TextButton(this, "- Settings", "white", "#29E0FF");
    settings->setFont(*buttonFont);
    settings->setGeometry(348, 422, 239, 34);
    connect(settings, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_settings_clicked()));
    settings->show();


    instructions = new TextButton(this, "- How To Play", "white", "#14DDFF");
    instructions->setFont(*buttonFont);
    instructions->setGeometry(348, 462, 289, 34);
    connect(instructions, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_instructions_clicked()));
    instructions->show();


    credits = new TextButton(this, "- Credits", "white", "#00DAFF");
    credits->setFont(*buttonFont);
    credits->setGeometry(348, 502, 214, 34);
    connect(credits, SIGNAL(clicked(QMouseEvent*)), this, SLOT(on_credits_clicked()));
    credits->show();

    //Display High Scores
    QFile file(filepath + "/gameData/highscores.txt");
    if (file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream stream(&file);

        ui->hsName1->setFont(*buttonFont);
        ui->hsName2->setFont(*buttonFont);
        ui->hsName3->setFont(*buttonFont);
        ui->hsName4->setFont(*buttonFont);
        ui->hsName5->setFont(*buttonFont);
        ui->hsNum1->setFont(*buttonFont);
        ui->hsNum2->setFont(*buttonFont);
        ui->hsNum3->setFont(*buttonFont);
        ui->hsNum4->setFont(*buttonFont);
        ui->hsNum5->setFont(*buttonFont);

        ui->hsName1->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsNum1->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsName2->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsNum2->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsName3->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsNum3->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsName4->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsNum4->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsName5->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
        ui->hsNum5->setText("<font color='lightskyblue'>" + stream.readLine(0) + "</font>");
    }
    file.close();
}

MainMenu::~MainMenu()
{
    delete ui;
}

void MainMenu::on_newGame_clicked()
{
    dynamic_cast<BaseWindow*>(this->parent())->showMainWindow("new game");
    this->deleteLater();
}

void MainMenu::on_loadGame_clicked()
{
    dynamic_cast<BaseWindow*>(this->parent())->showMainWindow("load game");
    this->deleteLater();
}

void MainMenu::on_settings_clicked()
{
    SettingsScreen *settings = new SettingsScreen(this, filepath);
    settings->show();
}

void MainMenu::on_instructions_clicked()
{
    InstructionScreen *instruct = new InstructionScreen(this, filepath);
    instruct->show();
}

void MainMenu::on_credits_clicked()
{
    CreditsScreen *credits = new CreditsScreen(this, filepath);
    credits->show();
}

void MainMenu::paintEvent(QPaintEvent *event)
{
    //Create the painter
    QPainter painter(this);

    //Draw background image
    //painter.setBrush(QBrush(Qt::white, Qt::SolidPattern));
    QRect imageRect(0,0,this->width(), this->height());
    painter.drawImage(imageRect, bg);

    painter.end();
}
